<template>
  <view class="goods-detail-box">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :interval="1500" :circular="true" :autoplay="true" class="pics-area">
      <swiper-item v-for="(pics,i) in goodsDetail.pics" :key="i">
        <image :src="pics.pics_mid_url"></image>
      </swiper-item>
    </swiper>
    <!-- 价格区域 -->
    <view class="goods-price-area">
      <view class="goods-price" v-if="goodsDetail.goods_price">¥{{goodsDetail.goods_price | tofixed}}</view>
      <view class="goods-detail">
        <view v-if="goodsDetail.goods_name" class="goods-name">
          {{goodsDetail.goods_name}}
        </view>
        <view class="goods-collect">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text class="collect">收藏</text>
        </view>
      </view>
      <view class="goods-transport">快递：免运费</view>
    </view>
    <!-- 商品详情页 -->
    <rich-text :nodes="goodsDetail.goods_introduce"></rich-text>
    <!-- 商品导航组件 -->
    <view class="goods_nav">
      <!-- fill 控制右侧按钮的样式 -->
      <!-- options 左侧按钮的配置项 -->
      <!-- buttonGroup 右侧按钮的配置项 -->
      <!-- click 左侧按钮的点击事件处理函数 -->
      <!-- buttonClick 右侧按钮的点击事件处理函数 -->
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="click" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goodsDetail: {},
        // 左侧按钮组的配置对象
        options: [{
          icon: 'shop',
          text: '店铺'
        }, {
          icon: 'cart',
          text: '购物车',
          info: 2
        }],
        // 右侧按钮组的配置对象
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    filters: {
      // 把数字处理为带两位小数点的数字
      tofixed(num) {
        console.log(num)
        return Number(num).toFixed(2)
      }
    },
    onLoad(option) {
      this.getGoodsDetail(option.goods_id)
    },
    methods: {
      click(content) {
        console.log(content.content.text === '购物车')
        if(content.content.text === "购物车") {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(content) {
        if(content.content.text === '加入购物车') {
          console.log("加入购物车")
        }
      },
      // 定义请求商品详情数据的方法
      async getGoodsDetail(goodsId) {
        await uni.$http.get('/api/public/v1/goods/detail', {
          'goods_id': goodsId
        }).then(res => {
          if (res.data.meta.status !== 200) {
            return uni.$showMsg()
          }
          // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
          // 将 webp 的后缀名替换为 jpg 的后缀名 -- 解决ios不支持的问题
          res.data.message.goods_introduce = res.data.message.goods_introduce.replace(/<img/g,
            '<img style="display:block;" ').replace(/webp/g, 'jpg')

          this.goodsDetail = res.data.message
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .pics-area {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-price-area {
    padding: 15px 10px;

    .goods-price {
      font-size: 18px;
      color: red;
      margin: 10px 0px;
    }

    .goods-detail {
      display: flex;


      .goods-name {

        font-size: 13px;
        padding-right: 10px;

      }

      .goods-collect {
        width: 100px;
        display: flex;
        align-items: center;
        border-left: 1px solid #efefef;
        flex-direction: column;
        justify-content: space-between;

        .collect {
          font-size: 12px
        }
      }
    }

    .goods-transport {

      font-size: 12px;
      color: gray;
      margin: 10px 0px;
    }

  }
  
  .goods_nav {
    position: sticky;
    bottom: 0;
    z-index: 999;
  }
</style>
